package com.runchinaup.smartbelt.preferences;

import android.content.Context;
import android.content.SharedPreferences;

import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class ModernDeviceInfoPro {


    public static void save(Context context, ArrayList<String> modernBles) {
        SharedPreferences preferences = context.getSharedPreferences(
                "modernBles", Context.MODE_PRIVATE);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(baos);
            oos.writeObject(modernBles);
            String productBase64 = new String(Base64.encodeBase64(baos.toByteArray()));
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("modern", productBase64);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static ArrayList<String> read(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(
                "modernBles", Context.MODE_PRIVATE);
        String productBase64 = preferences.getString("modern", "");
        if (productBase64 == "") {
            return null;
        }
        byte[] base64 = Base64.decodeBase64(productBase64.getBytes());
        ByteArrayInputStream bais = new ByteArrayInputStream(base64);
        try {
            ObjectInputStream bis = new ObjectInputStream(bais);
            try {
                return (ArrayList<String>) bis.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
